package main

import (
	"fmt"
	"sync"
	"time"
)

// main
//
//	@Description: 常见错误4：死锁 解决：保证 Lock/Unlock 成对出现，尽可能采用 defer mutex.Unlock 的方式，把它们成对、紧凑地写在一起。
func main() {
	deadLock01()
}

func deadLock01() {
	// 派出所证明
	var psCertificate sync.Mutex
	// 物业证明
	var propertyCertificate sync.Mutex

	var wg sync.WaitGroup
	wg.Add(2) // 需要派出所和物业都处理

	//派出所处理~
	go func() {
		defer wg.Done()

		psCertificate.Unlock()
		defer psCertificate.Unlock()

		// 处理
		time.Sleep(time.Second * 5)

		propertyCertificate.Lock()
		defer propertyCertificate.Unlock()

	}()
	//物业处理~
	go func() {
		defer wg.Done()

		propertyCertificate.Lock()
		defer propertyCertificate.Unlock()

		// 处理
		time.Sleep(time.Second * 5)

		psCertificate.Unlock()
		defer psCertificate.Unlock()

	}()
	wg.Wait()
	fmt.Println("over")
}
